package com.qinggeng.project.system.controller;

import com.qinggeng.common.utils.poi.ExcelUtil;
import com.qinggeng.framework.aspectj.lang.annotation.Log;
import com.qinggeng.framework.aspectj.lang.enums.BusinessType;
import com.qinggeng.framework.web.controller.BaseController;
import com.qinggeng.framework.web.domain.AjaxResult;
import com.qinggeng.framework.web.page.TableDataInfo;
import com.qinggeng.project.system.domain.UserCart;
import com.qinggeng.project.system.service.IUserCartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;
/**
 * 用户购物车信息Controller
 *
 * @author qinggeng
 * @date 2020-06-16
 */
@RestController
@RequestMapping("/system/cart")
public class UserCartController extends BaseController
{
    @Autowired
    private IUserCartService userCartService;

    /**
     * 查询用户购物车信息列表
     */
    @PreAuthorize("@ss.hasPermi('system:cart:list')")
    @GetMapping("/list")
    public TableDataInfo list(UserCart userCart)
    {
        startPage();
        List<UserCart> list = userCartService.selectUserCartList(userCart);
        return getDataTable(list);
    }

    /**
     * 导出用户购物车信息列表
     */
    @PreAuthorize("@ss.hasPermi('system:cart:export')")
    @Log(title = "用户购物车信息", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(UserCart userCart)
    {
        List<UserCart> list = userCartService.selectUserCartList(userCart);
        ExcelUtil<UserCart> util = new ExcelUtil<UserCart>(UserCart.class);
        return util.exportExcel(list, "cart");
    }

    /**
     * 获取用户购物车信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:cart:query')")
    @GetMapping(value = "/{cartId}")
    public AjaxResult getInfo(@PathVariable("cartId") Long cartId)
    {
        return AjaxResult.success(userCartService.selectUserCartById(cartId));
    }

    /**
     * 新增用户购物车信息
     */
    @PreAuthorize("@ss.hasPermi('system:cart:add')")
    @Log(title = "用户购物车信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody UserCart userCart)
    {
        return toAjax(userCartService.insertUserCart(userCart));
    }

    /**
     * 修改用户购物车信息
     */
    @PreAuthorize("@ss.hasPermi('system:cart:edit')")
    @Log(title = "用户购物车信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody UserCart userCart)
    {
        return toAjax(userCartService.updateUserCart(userCart));
    }

    /**
     * 删除用户购物车信息
     */
    @PreAuthorize("@ss.hasPermi('system:cart:remove')")
    @Log(title = "用户购物车信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{cartIds}")
    public AjaxResult remove(@PathVariable Long[] cartIds)
    {
        return toAjax(userCartService.deleteUserCartByIds(cartIds));
    }
}
